package top.hcode.hoj.service.oj.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.hcode.hoj.common.result.CommonResult;
import top.hcode.hoj.mapper.DepMapper;
import top.hcode.hoj.mapper.MajorMapper;
import top.hcode.hoj.mapper.UserInfoMapper;
import top.hcode.hoj.pojo.entity.user.Dep;
import top.hcode.hoj.pojo.entity.user.Major;
import top.hcode.hoj.pojo.entity.user.UserInfo;
import top.hcode.hoj.service.oj.MajorService;

import java.util.List;

@Service
public class MajorServiceImpl implements MajorService {


    @Autowired
    private MajorMapper majorMapper;
    @Autowired
    private DepMapper depMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;


    @Override
    public CommonResult<List<Major>> depfandmajor(String oj,String selectedId) {
        return CommonResult.successResponse(majorMapper.depfandmajor(selectedId));

    }

    @Override
    public CommonResult<List<Major>> getLits() {
        List<Major> majors = majorMapper.selectList(null);
        if (majors.size() > 0) {
            for (Major major : majors) {
                Dep dep = new Dep();
                dep = depMapper.selectById(major.getDepid());
                major.setDepid(dep.getDepname());
            }
        }
        return CommonResult.successResponse(majors);
    }

    @Override
    public CommonResult<Void> addmajor(Major major) {
        String uuid20 = String.format("%d%019d", new java.util.Random().nextInt(9) + 1, new java.util.Random().nextLong()).substring(0, 20);
        major.setId(uuid20);
        if (major.getDepid() == null || major.getDepid().equals("")) {
            return  CommonResult.errorResponse("院系不可以为空");
        }
        if (major.getName() == null || major.getName().equals("")) {
            return  CommonResult.errorResponse("专业不可以为空");
        }
      List<Major> majors = majorMapper.selectList(null);
            boolean b = majors.stream().anyMatch(m ->
                    m.getDepid().equals(major.getDepid()) &&
                            m.getName().equals(major.getName())
            );
            if (b) {
                return  CommonResult.errorResponse("同一院系下专业不可重复");
            }
            int result = majorMapper.insert(major);
            if (result > 0) {
                return CommonResult.successResponse("添加成功");
            } else {
                return CommonResult.errorResponse("添加失败");
            }
    }

    @Override
    public CommonResult<Void> updatemajor(Major major) {
        if (major.getDepid() == null || major.getDepid().equals("")) {
            return  CommonResult.errorResponse("院系不可以为空");
        }
        if (major.getName() == null || major.getName().equals("")) {
            return  CommonResult.errorResponse("专业不可以为空");
        }
        List<Major> majors = majorMapper.selectList(null);
        boolean b = majors.stream().anyMatch(m ->
                m.getDepid().equals(major.getDepid()) &&
                        m.getName().equals(major.getName())
        );
        if (b) {
            return  CommonResult.errorResponse("同一院系下专业不可重复");
        }
        int result = majorMapper.updateById(major);
        if (result > 0) {
            return CommonResult.successResponse("修改成功");
        } else {
            return CommonResult.errorResponse("修改失败");
        }
    }

    @Override
    public int deleteMojor(String id) {
        return majorMapper.deleteById(id);
    }

    @Override
    public CommonResult<Void> deletemajor(Major major) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("majorid", major.getId()); // WHERE majorid = #{majorid}
        List<UserInfo> userList = userInfoMapper.selectList(wrapper);
        if (userList.size() > 0) {
            return CommonResult.errorResponse("已经绑定学生不可删除");
        }else {
          majorMapper.deleteById(major.getId());
          return CommonResult.successResponse();
        }
    }
}
